<?php

namespace App\AgentAdmin\Controllers;

use App\Admin\Repositories\AgentPrice;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\SystemConfig;
use Dcat\Admin\Admin;
use App\Models\AgentPrice as AgentPriceModel;
use Dcat\Admin\Widgets\Alert;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Column;
use App\Services\ConfigService;

class AgentPriceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $config = ConfigService::videoPriceConfig();
        $conf = AgentPriceModel::firstOrCreate(
            ['agent_id'=>Admin::user()->id],
            [
                'agent_id'=>Admin::user()->id,
                'min_price'=>$config['content']['min_price'],
                'max_price'=>$config['content']['max_price'],
            ]
        );
        return Grid::make(new AgentPrice(), function (Grid $grid) {
            
            $grid->model()->where('agent_id',(Admin::user()->id));
            // $grid->column('id')->sortable();
            // $grid->column('agent_id')->filter();
            $grid->column('min_price');
            $grid->column('max_price');
            // $grid->column('created_at')->sortable();
            // $grid->column('updated_at')->sortable();
        
            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->disableRowSelector();
            $grid->disableBatchActions();
            $grid->disableDeleteButton();
            $grid->disableRefreshButton();
            // 禁用详情按钮
            $grid->disableViewButton();
            //头部
            $grid->header(function ($collection){
                $alert = Alert::make( '固定金额请把最低和最高金额填写成一样的','提示');
                return $alert;
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AgentPrice(), function (Show $show) {
            $show->field('id');
            $show->field('agent_id');
            $show->field('min_price');
            $show->field('max_price');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    public function edit($id, Content $content)
        {
            $config = ConfigService::videoPriceConfig();
            $alert = Alert::make( "价格区间 【￥{$config['content']['min_price']} - ￥{$config['content']['max_price']}】固定金额请把最低和最高金额填写成一样的",'提示');
            return $content
                ->header('打赏价格设置')
                ->body(
                function (Row $row) use($id,$alert){
                    $row->column(6, function (Column $column) use($id,$alert) {
                        $column->row($alert->danger());
                        $column->row($this->form()->edit($id));
                    });
                });
        }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $config = ConfigService::videoPriceConfig();
        $conf = AgentPriceModel::firstOrCreate(
            ['agent_id'=>Admin::user()->id],
            [
                'agent_id'=>Admin::user()->id,
                'min_price'=>$config['content']['min_price'],
                'max_price'=>$config['content']['max_price'],
            ]
        );
        return  Form::make($conf, function (Form $form) use($config){
            $form->currency('min_price')
            ->attribute('min', $config['content']['min_price'])
            ->attribute('max', $config['content']['max_price']);;
            $form->currency('max_price')
            ->attribute('min', $config['content']['min_price'])
            ->attribute('max', $config['content']['max_price']);
        
            $form->disableHeader();
            $form->footer(function ($footer) {

                // 去掉`查看`checkbox
                $footer->disableViewCheck();
            
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
            
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
            //编辑缓存同步
            $form->saved(function(Form $form){
                cache()->forget("video_price_conf:agent_".Admin::user()->id);
            });
        });
    }
}
